<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址类型" prop="addressType">
        <el-select filterable v-model="queryParams.addressType" placeholder="请选择地址类型" clearable>
          <el-option
            v-for="dict in dict.type.addressType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['extend:jcReceivingParty:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['extend:jcReceivingParty:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['extend:jcReceivingParty:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receivingpartyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="客户名称" align="center" prop="dtMap.customerIdDtLabel"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="地址类型" align="center" prop="addressType" width="100">
        <template #default="scope">
          <dict-tag :options="dict.type.addressType" :value="scope.row.addressType"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contactperson" width="100"/>
      <el-table-column label="电话" align="center" prop="iphone" width="120"/>
      <el-table-column label="经纬度" align="center" prop="ltl"/>
      <el-table-column label="行政区域" align="center" prop="jcZoneIdDtLabel"/>
      <el-table-column label="详细地址" align="center" prop="detaileAddress"/>
      <el-table-column label="状态" align="center" prop="status" width="70">
        <template #default="scope">
          <dict-tag :options="dict.type.CommDataStatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="是否默认" align="center" prop="isDefault" width="90">
        <template #default="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isDefault"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['extend:jcReceivingParty:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['extend:jcReceivingParty:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收发货地址对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="24">
            <a-map-index @map-position="getMapPosition"></a-map-index>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细地址" prop="detaileAddress">
              <el-input v-model="form.detaileAddress" placeholder="请输入详细地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址类型" prop="addressType">
              <el-select filterable v-model="form.addressType" placeholder="请选择地址类型">
                <el-option
                  v-for="dict in dict.type.addressType"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactperson">
              <el-input v-model="form.contactperson" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="iphone">
              <el-input v-model="form.iphone" placeholder="请输入电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经纬度" prop="ltl">
              <el-input v-model="form.ltl" placeholder="请输入经纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="orderby">
              <el-input v-model="form.orderby" placeholder="请输入排序"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行政区域" prop="jcZones">
              <el-cascader :props="regionProps" v-model="form.jcZones" style="width: 100%;"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址代码" prop="zoneCode">
              <el-input v-model="form.zoneCode" placeholder="请输入地址代码" readonly="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addReceivingparty, delReceivingparty, getReceivingparty, listReceivingparty, updateReceivingparty } from '@/api/extend/receivingparty'
import { getCustomer } from '@/api/extend/customer'
import AMapIndex from '@/components/AMap/index.vue'
import { getChildZone } from '@/api/kysys/zone'

export default {
  name: 'Receivingparty',
  dicts: ['sys_yes_no', 'addressType', 'CommDataStatus'],
  components: {
    AMapIndex
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收发货地址表格数据
      receivingpartyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createName: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        customerId: null,
        name: null,
        status: null,
        zoneCode: null,
        address: null,
        addressType: null,
        contactperson: null,
        iphone: null,
        ltl: null,
        orderby: null,
        jcOrganizationId: null,
        jcZoneId: null,
        detaileAddress: null,
        isDefault: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "客户ID不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        addressType: [
          { required: true, message: "地址类型不能为空", trigger: "change" }
        ],
        contactperson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        iphone: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
        ltl: [
          { required: true, message: "经纬度不能为空", trigger: "blur" }
        ],
        jcOrganizationId: [
          { required: true, message: "组织机构不能为空", trigger: "blur" }
        ],
        jcZones: [
          { required: true, message: "行政区域ID不能为空", trigger: "blur" }
        ],
        detaileAddress: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
      },
      customer: null,
      customerId: null,
      regionProps: {
        lazy: true,
        lazyLoad (node, resolve) {
          let zid = node.value || 0
          getChildZone({zid: zid}).then(response => {
            resolve(response.data)
          })
        },
        checkStrictly: true,
        value: 'id',
        label: 'name',
      }
    }
  },
  created() {
    const customerId = this.$route.params && this.$route.params.customerId
    if (customerId) {
      this.customerId = customerId
      this.getCustomerInfo(customerId)
    }
    this.getList()
  },
  methods: {
    getCustomerInfo(customerId) {
      getCustomer(customerId).then(response => {
        this.customer = response.data
      })
    },
    /** 查询收发货地址列表 */
    getList() {
      this.loading = true
      this.queryParams.customerId = this.customerId
      listReceivingparty(this.queryParams).then(response => {
        this.receivingpartyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        customerId: null,
        name: null,
        status: null,
        zoneCode: null,
        address: null,
        addressType: null,
        contactperson: null,
        iphone: null,
        ltl: null,
        orderby: null,
        jcOrganizationId: null,
        jcZoneId: null,
        detaileAddress: null,
        isDefault: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加收发货地址'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReceivingparty(id).then(response => {
        this.form = response.data
        if (this.form.jcZones) {
          this.form.jcZones = this.form.jcZones.split(',')
        }
        this.open = true
        this.title = '修改收发货地址'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.customerId = this.customerId
          if (this.form.jcZones) {
            this.form.jcZoneId = this.form.jcZones[this.form.jcZones.length - 1]
            this.form.jcZones = this.form.jcZones.join(',')
          }
          if (this.form.id != null) {
            updateReceivingparty(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addReceivingparty(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除收发货地址编号为"' + ids + '"的数据项？').then(function() {
        return delReceivingparty(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('extend/receivingparty/export', {
        ...this.queryParams
      }, `receivingparty_${new Date().getTime()}.xlsx`)
    },
    getMapPosition(data) {
      this.form.address = data.address
      this.form.detaileAddress = data.address
      this.form.ltl = data.latitude + ',' + data.longitude
      if (data.addressComponent) {
        this.form.zoneCode = data.addressComponent.adcode
      }
    }
  }
}
</script>
